﻿@model Bricks.Business.Models.Account.OrgInfo

@{
    ViewBag.Title = "机构授权";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

<script type="text/javascript">
    $(function () {
        //当页面首次刷新的时候执行的事件
        initOrgList();

        //$('#purviewUIEmbed').panel({
        //    //href: '/Account/Index?MenuID=B4965782-329A-4A33-82EC-A8353E7054EB',
        //    href: '/Purview/Purview'
        //});

        //载入嵌入页面（权限页面）
        $.LoadEmbed('#purviewUIEmbed', '/Purview/Purview');
    });
    //实现对DataGird控件的绑定操作

    function initOrgList(queryData) {
        $('#selectedOrgDataGrid').datagrid({
            url: '/Purview/GetSelectedOrgList?OrgID=' + $("#hSelectedOrgID").val(),
            //title: '机构管理',  //标识
            //iconCls: 'icon-user',
            //width: 'auto',
            //height: 500,
            nowrap: true,
            autoRowHeight: false,
            singleSelect: true,
            striped: true,
            collapsible: true,
            pagination: false,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            queryParams: queryData,  //异步查询的参数
            toolbar: '#selectedOrgToolBar',
            columns: [[
                { title: '@Html.LabelFor(model => model.OrgName)', field: 'OrgName', width: 165, sortable: false },
            ]],
            onClickRow: function (index, row) {
                //以下方法可行
                var selectedRow = $("#selectedOrgDataGrid").datagrid("getSelected");
                if (selectedRow) {
                    $('#purviewDataGrid').datagrid('load', { instanceID: selectedRow.ID });
                }
                //$.EasyuiSlide("点击事件测试");
            }
        });
    }
    var OrgPurview = {
        //选择机构
        ChooseOrg: function () {
            //var row = $("#selectedOrgDataGrid").datagrid("getSelected");
            //if (row) {
            //    OrgInfo.Mutual("修改", "/OrgInfo/Edit?ID=" + row.ID);
            //}
            //else { $.EasyuiSlide("请选择一条记录"); }

            //var arr = $('#selectedOrgDataGrid').datagrid('getSelections');
            //if (arr.length > 0) {
            //}
            var orgList = $('#selectedOrgDataGrid').datagrid('getRows');  //获取列表数据
            //var orgListJson = JSON.stringify(orgList);  //转为json格式
            var orgIDs = "";
            if (orgList.length > 0) {
                for (var i = 0; i < orgList.length; i++) {
                    if (orgIDs.length == 0)
                        orgIDs = orgList[i].ID;
                    else
                        orgIDs = orgIDs + "," + orgList[i].ID;
                }
            }
            OrgPurview.Mutual("选择机构", "/Purview/ChooseOrg?orgIDs=" + orgIDs);  //弹出对话框
        },

        //实体操作
        Mutual: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 320, maximizable: true,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-chooseOrg",
                        //url: "/Purview/ChooseOrgPost?selectedOrgIDs=" + $("#hOrgIDs").val(),  //eg：Purview/ChooseOrg
                        url: "/Purview/ChooseOrgPost",  //eg：Purview/ChooseOrg
                        success: function (result) {
                            //debugger;
                            var orgIDs = $("#hOrgIDs").val();
                            //OrgPurview.Reload();
                            //$.EasyuiMsg(orgIDs);
                            //document.getElementById("hSelectedOrgID").value = orgIDs;  //给隐藏域字段赋值
                            //OrgPurview.Reload();
                            $("#selectedOrgDataGrid").datagrid("reload", { orgID: orgIDs });  //刷新左边机构列表
                            $("#purviewDataGrid").datagrid("reload");  //刷新右边权限路径列表（实际运行疑问：为什么这个语句在下方，而却先执行呢？）
                        }
                    });
                },
                onLoad: function () { OrgPurview.FormInit(); }
            });
        },

        //刷新重载
        Reload: function () {
            $("#selectedOrgDataGrid").datagrid("reload");
        },

        //弹窗初始化
        FormInit: function () {
            //定义父级的机构树
            //$("#PID").combotree({
            //    width: 150, url: "/OrgInfo/GetOrgComboTree",
            //    onLoadSuccess: function (node, data) {
            //        var nodeid = $("#ID").val();
            //        if (nodeid) {
            //            var etree = $("#PID").combotree("tree");
            //            etree.tree("remove", etree.tree('find', nodeid).target);
            //        }
            //    }
            //});
            ////定义机构类型枚举下拉
            //$("#OrgTypeNum").combobox({
            //    editable: false, width: 150, panelHeight: "auto",
            //    data: [
            //        { value: "0", text: "未指定" },
            //        { value: "1", text: "公司" },
            //        { value: "2", text: "部门" },
            //        { value: "3", text: "岗位" }
            //    ]
            //});
        }
    }
</script>

<table>
    <tr>
        <form id="form-orgPurview" method="post">
            <td>
                <div region="west" data-options="border:false" style="width: 200px; height: 387px;">
                    <div class="easyui-panel" title="机构选择">
                        <!--已选机构列表-->
                        <table id="selectedOrgDataGrid" style="height: 358px;"></table>
                        <!--已选机构工具栏-->
                        <div id="selectedOrgToolBar">
                            <a href='javascript:void(0)' class='easyui-linkbutton' onclick='OrgPurview.ChooseOrg()' data-options='plain:true,iconCls:"icon-application_lightning"'>选择机构</a>
                        </div>
                    </div>
                </div>
            </td>

            <!--右边为机构数表，可勾选用户所属机构和勾选对应权限-->
            <td>
                @*<div region="center" style="border-left: 0px; width: 420px; height: 387px;">
                    <div class="easyui-panel" title="机构授权">
                        @Html.Raw(ViewBag.PurviewUI)
                    </div>
                </div>*@

                <div region="center" style="border-left: 0px; width: 420px; height: 387px;">
                    <div id="purviewUIEmbed" class="easyui-panel" fit="true"></div>
                </div>
            </td>
        </form>
    </tr>
</table>

<input type="hidden" id="hSelectedOrgID" value="@ViewBag.SelectedOrgID" />
<input type="hidden" id="hConfigType" value="@ViewBag.ConfigType" />
